package com.jinkaimen.kqb.ui.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.ht.baselibs.core.EventCenter;
import com.ht.baselibs.core.EventCode;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseFragment;
import com.ht.baselibs.utils.ActivityUtils;
import com.ht.baselibs.utils.SharedPreferencesUtils;
import com.jinkaimen.kqb.R;
import com.jinkaimen.kqb.constant.Constants;
import com.jinkaimen.kqb.dao.AccountDao;
import com.jinkaimen.kqb.manager.ApiManager;
import com.jinkaimen.kqb.ui.activity.addmerchant.WebViewActivity;
import com.jinkaimen.kqb.ui.activity.h5toapp.GoUpgradeActivity;
import com.jinkaimen.kqb.ui.dialog.TipsDialog;
import com.jinkaimen.kqb.utils.AppUtils;

import de.greenrobot.event.EventBus;
import retrofit2.Call;
import retrofit2.Response;


/**

 */
public class SelfNewFragment extends BaseFragment {


    private TipsDialog mTipsDialog;

    @Override
    protected boolean hasEvent() {
        return true;
    }

    @Override
    protected View initContentView() {
        return LayoutInflater.from(mContext).inflate(R.layout.frag_self_new, null);
    }

    @Override
    protected void initChildView() {

    }


    @Override
    protected boolean isStaticPage() {
        return true;
    }


    @Override
    protected void setChildViewListener() {
        findViewById(R.id.ll_frag_my_bank_card_info).setOnClickListener(this);
        findViewById(R.id.ll_frag_my_real_name_info).setOnClickListener(this);
        findViewById(R.id.ll_frag_use_help).setOnClickListener(this);
        findViewById(R.id.ll_frag_log_out_self).setOnClickListener(this);
    }

    @Override
    protected void initTitleBar() {

    }


    @Override
    protected void onEvent(int eventCode, Object data) {
        switch (eventCode) {
            case EventCode.EVENT_REFRESH_SHENHE_STATE:

                break;

        }
    }

    @Override
    public void onClick(View v) {
        Bundle bundle = new Bundle();
        String token = SharedPreferencesUtils.getString(Constants.TOKEN, "");
        switch (v.getId()) {
            case R.id.ll_frag_my_bank_card_info:
                bundle.putString(WebViewActivity.TITLE, "我的银行卡");
                bundle.putString(WebViewActivity.URL, "http://hss.qianbaojiajia.com/earn/bankList" + Constants.TOKEN_TITLE + token);
                ActivityUtils.startActivity(mContext, WebViewActivity.class, bundle);
                break;
            case R.id.ll_frag_my_real_name_info:
                bundle.putString(WebViewActivity.TITLE, "实名认证");
                bundle.putString(WebViewActivity.URL, "http://hss.qianbaojiajia.com/earn/certification" + Constants.TOKEN_TITLE + token);
                ActivityUtils.startActivity(mContext, WebViewActivity.class, bundle);
                break;
            case R.id.ll_frag_use_help:
                bundle.putString(WebViewActivity.TITLE, "使用帮助");
                bundle.putString(WebViewActivity.URL, "http://hss.qianbaojiajia.com/earn/help");
                ActivityUtils.startActivity(mContext, WebViewActivity.class, bundle);
                break;
            case R.id.ll_frag_log_out_self:
                String cid = SharedPreferencesUtils.getString(Constants.CID, "");
                Log.e("cid == ", cid);
                logOut();
                break;

        }

    }

    @Override
    public void onResume() {
        super.onResume();
    }

    private void logOut() {
        mTipsDialog = new TipsDialog(mContext);
        mTipsDialog.setTitileHint("确认退出当前账号？\n");
        mTipsDialog.show();
        mTipsDialog.setOnDialogTipsClickListener(new TipsDialog.OnDialogTipsClickListener() {

            @Override
            public void doEnter() {
                mTipsDialog.dismiss();

            }

            @Override
            public void doCancel() {
                mTipsDialog.dismiss();
                mContext.showLoadingDialogCanotCancle();
                ApiManager.getApi().logout(
                        "HSS001008"
                ).enqueue(new NetCallBack<String>() {
                    @Override
                    protected void onSuccess(Call<String> call, Response<String> response) {
                        AppUtils.logout();
                        AccountDao.getInstance().clear();
                        EventBus.getDefault().post(new EventCenter(EventCode.EVENT_CODE_START_LOGOUT));
//                        sc_self_all.post(new Runnable() {//滑动到顶部
//                            @Override
//                            public void run() {
//                                sc_self_all.fullScroll(ScrollView.FOCUS_UP);
//                            }
//                        });
                    }

                    @Override
                    protected void hiddenProgress() {
                        mContext.hideLodingDialog();
                    }

                    @Override
                    protected boolean isNullNeedCallback() {
                        return true;
                    }
                });
            }
        });
    }

}